package com.guchenbo.example.encoding;

import java.io.UnsupportedEncodingException;

/**
 * @Author penglai
 * @Date 2016/10/26.
 */
public class HexUtils {
    public static void toHex(char[] cs, String s) {
        StringBuffer sb = new StringBuffer(s).append(" --> ");
        for (char c : cs) {
            sb.append(Integer.toHexString(c)).append(" ");
        }
        System.out.println(sb.toString());
    }

    public static void toHex(byte[] bs, String s) {
        StringBuffer sb = new StringBuffer(s).append(" --> ");
        for (byte c : bs) {
            sb.append(Integer.toHexString((c & 0xff))).append(" ");
        }
        System.out.println(sb.toString());
    }

    public static void toHex(String str, byte[] bs, String charset) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer(str).append("[")
        .append(charset).append("]").append(" --> ");
        for (byte c : bs) {
            sb.append(Integer.toHexString((c & 0xff))).append(" ");
        }
        sb.append(new String(bs, charset));
        System.out.println(sb.toString());
    }
}
